<template>
  <view class="container">
    <!-- 标题栏 -->
    <view class="header">
      <view class="back-button" @click="goBack">
        <uni-icons class="back-icon" custom-prefix="iconfont" type="icon-fanhui" size="18"></uni-icons>
      </view>
      <view class="title">{{ title }}</view>
    </view>
    
    <!-- 文献内容区 -->
    <scroll-view class="content-scroll" scroll-y>
      <!-- 文献信息 -->
      <view class="reference-info">
        <view class="info-item">
          <text class="info-label">作者：</text>
          <text class="info-value">{{ author }}</text>
        </view>
        <view class="info-item">
          <text class="info-label">出版日期：</text>
          <text class="info-value">{{ publishDate }}</text>
        </view>
        <view class="info-item">
          <text class="info-label">来源：</text>
          <text class="info-value">{{ source }}</text>
        </view>
      </view>
      
      <!-- 摘要 -->
      <view class="section">
        <view class="section-title">摘要</view>
        <view class="section-content">{{ abstract }}</view>
      </view>
      
      <!-- 正文内容 -->
      <view class="section" v-for="(section, index) in contentSections" :key="index">
        <view class="section-title">{{ section.title }}</view>
        <view class="section-content">{{ section.content }}</view>
      </view>
      
      <!-- 参考文献 -->
      <view class="section" v-if="citations.length > 0">
        <view class="section-title">参考文献</view>
        <view class="citation-list">
          <view class="citation-item" v-for="(citation, index) in citations" :key="index">
            [{{ index + 1 }}] {{ citation }}
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'

// 获取页面传递的参数
const title = ref('文献标题')
const contentId = ref('')
const parentId = ref('')

// 文献数据
const author = ref('未知')
const publishDate = ref('--')
const source = ref('--')
const abstract = ref('暂无摘要')
const contentSections = ref([])
const citations = ref([])

// 初始化
onMounted(() => {
  // 获取页面参数
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  const options = currentPage.$page.options
  
  if (options) {
    contentId.value = options.id || ''
    title.value = decodeURIComponent(options.title || '文献详情')
    parentId.value = options.parentId || ''
    
    // 获取文献数据
    fetchReferenceData(contentId.value)
  }
})

// 获取文献数据
const fetchReferenceData = async (id) => {
  try {
    // 模拟API请求
    // const response = await uni.request({
    //   url: `your-api-endpoint/references/${id}`,
    //   method: 'GET'
    // })
    
    // 模拟数据
    
    
    console.log('Reference data fetched for id:', id)
  } catch (error) {
    console.error('Failed to fetch reference data:', error)
    uni.showToast({
      title: '获取文献内容失败',
      icon: 'none'
    })
  }
}

// 更新阅读进度
const updateReadingProgress = async () => {
  try {
    // 模拟API请求
    // await uni.request({
    //   url: 'your-api-endpoint/reading-progress',
    //   method: 'POST',
    //   data: {
    //     contentId: contentId.value,
    //     timestamp: new Date().getTime()
    //   }
    // })
    console.log('Reading progress updated')
  } catch (error) {
    console.error('Failed to update reading progress:', error)
  }
}

// 返回上一页
const goBack = () => {
  // 更新阅读进度
  updateReadingProgress()
  uni.navigateBack()
}
</script>

<style scoped lang="scss">
.container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-top: var(--status-bar-height);
  background-color: #f5f5f5;
}

.header {
  height: 90rpx;
  display: flex;
  align-items: center;
  padding: 0 30rpx;
  background-color: #ffffff;
  border-bottom: 2rpx solid rgba(229, 229, 229, 1);
}

.back-button {
  width: 60rpx;
  height: 60rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-icon {
  font-size: 40rpx;
  color: #333;
}

.title {
  font-size: 32rpx;
  font-weight: bold;
  margin-left: 20rpx;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.content-scroll {
  flex: 1;
  margin: 20rpx;
  background-color: #ffffff;
  border-radius: 12rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.reference-info {
  padding: 30rpx;
  border-bottom: 1rpx solid rgba(229, 229, 229, 0.6);
}

.info-item {
  display: flex;
  margin-bottom: 10rpx;
}

.info-label {
  font-weight: bold;
  width: 180rpx;
  color: #666;
}

.info-value {
  flex: 1;
  color: #333;
}

.section {
  padding: 30rpx;
  border-bottom: 1rpx solid rgba(229, 229, 229, 0.6);
  
  &:last-child {
    border-bottom: none;
  }
  
  .section-title {
    font-size: 32rpx;
    font-weight: bold;
    margin-bottom: 20rpx;
    color: #333;
  }
  
  .section-content {
    font-size: 30rpx;
    line-height: 1.8;
    color: #333;
    text-align: justify;
  }
}

.citation-list {
  padding-left: 20rpx;
}

.citation-item {
  font-size: 28rpx;
  line-height: 1.6;
  color: #666;
  margin-bottom: 10rpx;
}
</style> 